const db = require("./../server/db");
const BASE_URL = process.env.API_HOST + ":" + process.env.API_PORT;
console.log(BASE_URL);

module.exports = {
  async getBanners(res) {
    const connection = await db.getConnection();
    try {
      // 查询总数
      const [countResult] = await connection.query(
        "SELECT COUNT(*) as total FROM banners"
      );

      // 查询分页数据
      const [banners] = await connection.query("SELECT * FROM banners");
      banners.forEach((element) => {
        element.image = BASE_URL + element.image;
      });
      // 标准化响应
      return res.json({
        code: 0,
        success: true,
        data: banners,
        pagination: {
          total: countResult[0].total,
        },
      });
    } catch (queryErr) {
      console.error("查询错误:", queryErr);
      return res.status(500).json({
        success: false,
        message: "查询数据库失败",
        error: queryErr.message,
      });
    } finally {
      connection.release();
      console.log("数据库连接已释放");
    }
  },

  async getNotices(res) {
    const connection = await db.getConnection();
    try {
      // 查询总数
      const [countResult] = await connection.query(
        "SELECT COUNT(*) as total FROM notices"
      );

      // 查询分页数据
      const [notices] = await connection.query("SELECT * FROM notices");
      // 标准化响应
      return res.json({
        code: 0,
        success: true,
        data: notices,
        pagination: {
          total: countResult[0].total,
        },
      });
    } catch (queryErr) {
      console.error("查询错误:", queryErr);
      return res.status(500).json({
        success: false,
        message: "查询数据库失败",
        error: queryErr.message,
      });
    } finally {
      connection.release();
      console.log("数据库连接已释放");
    }
  },

  async getCategorys(res) {
    const connection = await db.getConnection();
    try {
      // 查询总数
      const [countResult] = await connection.query(
        "SELECT COUNT(*) as total FROM categories"
      );

      // 查询分页数据
      const [categorys] = await connection.query(
        "SELECT * FROM categories LIMIT ? OFFSET ?",
        [4, 0]
      );

      categorys.forEach((element) => {
        if (!element.image) return;
        element.image = BASE_URL + element.image;
      });
      // 标准化响应
      return res.json({
        code: 0,
        success: true,
        data: categorys,
        pagination: {
          total: countResult[0].total,
        },
      });
    } catch (queryErr) {
      console.error("查询错误:", queryErr);
      return res.status(500).json({
        success: false,
        message: "查询数据库失败",
        error: queryErr.message,
      });
    } finally {
      connection.release();
      console.log("数据库连接已释放");
    }
  },

  async getFeatureds(res, page, size, offset) {
    const connection = await db.getConnection();
    try {
      // 查询总数
      const [countResult] = await connection.query(
        "SELECT COUNT(*) as total FROM products"
      );

      // 查询分页数据
      const [featureds] = await connection.query(
        "SELECT * FROM products LIMIT ? OFFSET ?",
        [5, 0]
      );

      featureds.forEach((element) => {
        element.image = BASE_URL + element.image;
      });
      // 标准化响应
      return res.json({
        code: 0,
        success: true,
        data: featureds,
        pagination: {
          page,
          size,
          total: countResult[0].total,
          totalPages: Math.ceil(countResult[0].total / size),
        },
      });
    } catch (queryErr) {
      console.error("查询错误:", queryErr);
      return res.status(500).json({
        success: false,
        message: "查询数据库失败",
        error: queryErr.message,
      });
    } finally {
      connection.release();
      console.log("数据库连接已释放");
    }
  },
  async getBestsellers(res, page, size, offset) {
    const connection = await db.getConnection();
    try {
      // 查询总数
      const [countResult] = await connection.query(
        "SELECT COUNT(*) as total FROM products"
      );

      // 查询分页数据
      const [bstsellers] = await connection.query(
        "SELECT * FROM products LIMIT ? OFFSET ?",
        [10, 0]
      );

      bstsellers.forEach((element) => {
        element.image = BASE_URL + element.image;
      });
      // 标准化响应
      return res.json({
        code: 0,
        success: true,
        data: bstsellers,
        pagination: {
          page,
          size,
          total: countResult[0].total,
          totalPages: Math.ceil(countResult[0].total / size),
        },
      });
    } catch (queryErr) {
      console.error("查询错误:", queryErr);
      return res.status(500).json({
        success: false,
        message: "查询数据库失败",
        error: queryErr.message,
      });
    } finally {
      connection.release();
      console.log("数据库连接已释放");
    }
  },
  async getProducts(res, page, size, offset) {
    const connection = await db.getConnection();
    console.log("数据库连接成功");

    try {
      // 查询总数
      const [countResult] = await connection.query(
        "SELECT COUNT(*) as total FROM products"
      );

      // 查询分页数据
      const [products] = await connection.query(
        "SELECT * FROM products LIMIT ? OFFSET ?",
        [size, offset]
      );

      products.forEach((element) => {
        element.image = BASE_URL + element.image;
      });

      // 标准化响应
      return res.status(200).json({
        code: 0,
        success: true,
        data: products,
        pagination: {
          page,
          size,
          total: countResult[0].total,
          totalPages: Math.ceil(countResult[0].total / size),
        },
      });
    } catch (queryErr) {
      console.error("查询错误:", queryErr);
      return res.status(500).json({
        success: false,
        message: "查询数据库失败",
        error: queryErr.message,
      });
    } finally {
      connection.release();
      console.log("数据库连接已释放");
    }
  },
};
